#include "ResourceElement.h"
//necessary
#include "memory-tools.h"

//TODO testing
PresourceElement initResourceElement(ResourceClassType _class,char* _name,bool _isAbstract, bool _isStatic, Pschema _schema,PidentifierList _readGroup,PidentifierList _writeGroup,unsigned short _vararg,PlocalResourceHTCell _localEnvironment,LocalResourceList _formalParams){
	PresourceElement result=(PresourceElement)mallocMemory(sizeof(ResourceElement),"ResourceElement.c:initResourceElement:1");
	result->class=_class;
	result->formalParams=_formalParams;
	result->isAbstract=_isAbstract;
	result->isStatic=_isStatic;
	result->localEnvironment=_localEnvironment;
	result->name=_name;
	result->oid=0;//TODO is OID really important????
	result->readGroup=_readGroup;
	result->schema=_schema;
	result->vararg=_vararg;
	result->writeGroup=_writeGroup;
	return result;
}

//TODO testing
void freeResourceElement(PresourceElement pobj){
	//class: it is a value field so it's not mandatory to free it
	//isAbstract: it is a value field so it's not mandatory to free it
	//isStatic: it is a value field so it's not mandatory to free it
	//formal params: it is a pointer to a elementin local resources, so there is no need to deallocates it
	//local environment: the only thing that must be deallocated (owned only by the resource) is the local environment
	freeLocalResourceHashTable(&pobj->localEnvironment);
	//name: it is part of the syntax tree, so there is no need to free it
	//read group: it a pointer to a list of label identifiers, so there is need to frees it
	//TODO deallocates correctly read group
	//write group: it a pointer to a list of label identifiers, so there is need to frees it
	//TODO deallocates correctly write group
	//schema: it must be freed
	freeSchema(pobj->schema);
	//var args: it is a number so there is no need to free it
}

void printResourceElement(FILE* f,PresourceElement pobj){
	printResourceClassType(f,pobj->class);
	if (pobj->isAbstract){
		fprintf(f," Abs");
	}
	if (pobj->isStatic){
		fprintf(f," Sta");
	}
	fprintf(f," ");
	fprintf(f,"%s",pobj->name);
}

//TODO testing
void generateDOTResourceElement(FILE* f,PresourceElement pobj,int* nodenumber,int* cellnumber,int format){
	//class
	printResourceClassType(f,pobj->class);
	//abstractness
	if (pobj->isAbstract){
		fprintf(f," ABS");
	}
	//staticness
	if (pobj->isStatic){
		fprintf(f," STA");
	}
	fprintf(f,"\n");
	//formal params
	//TODO you might want to add formal params...
	//local environment
	//TODO you might want to add local environment...
	//name: the name is already printed on the HTcell
	//read group
	//TODO you might want to add read groups...
	//writegroup
	//TODO you might want to add write groups...
	printSchema(f,pobj->schema);
	//TODO you might want to add schema...
	//var args
	if (pobj->class==RESOURCECLASSTYPE_FUNCTION){
		fprintf(f,"#params=%su",pobj->vararg);
	}
	fprintf(f,"\n");
}
